package com.smartlink.app.activity;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.smartlink.app.R;
import com.smartlink.app.dao.ClassRoomInfoDao;
import com.smartlink.app.fragment.ClassRoomFragment;
import com.smartlink.app.fragment.StudentFragment;
import com.smartlink.app.fragment.TeachFragment;
import com.smartlink.app.fragment.UserFragment;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private FrameLayout fl_main_content, fl_main_nativity;
    private android.support.v4.app.FragmentTabHost tab_host;
    private LayoutInflater layoutInflater;
    private Class[] fragments = new Class[]{ClassRoomFragment.class, TeachFragment.class, UserFragment.class};
    private String[] tabTexts = new String[]{"智能教室", "教学管理", "个人信息"};
    private Integer[] tabImages = new Integer[]{R.drawable.sun_black, R.drawable.book_black, R.drawable.user_black};
    private SharedPreferences sharedPreferences;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sharedPreferences = getSharedPreferences("main", MODE_PRIVATE);
        int userType = sharedPreferences.getInt("userType", 0);
        if (userType == 999) {
            fragments[1] = TeachFragment.class;
        } else {
            fragments[1] = StudentFragment.class;
        }
        bindViews();
    }

    @Override
    protected void onDestroy() {
        //清除Shareperferce
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove("classRoomId");
        editor.remove("classRoomName");
        editor.apply();
        //清除数据库数据
        new ClassRoomInfoDao(this).deleteInfos();
        Log.i(TAG, "onDestroy: cleart over.....");
        super.onDestroy();
    }

    private void bindViews() {
        layoutInflater = getLayoutInflater();
        fl_main_content = (FrameLayout) findViewById(R.id.fl_main_content);
        tab_host = (android.support.v4.app.FragmentTabHost) findViewById(R.id.tab_host);
        fl_main_nativity = (FrameLayout) findViewById(R.id.fl_main_nativity);
        tab_host.setup(this, getSupportFragmentManager(), R.id.fl_main_content);
        int count = fragments.length;
        for (int i = 0; i < count; i++) {
            //为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = tab_host.newTabSpec(tabTexts[i]).setIndicator(getTabItemView(i));
            //将Tab按钮添加进Tab选项卡中
            tab_host.addTab(tabSpec, fragments[i], null);
            //设置Tab按钮的背景
            //tab_host.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);
        }
    }

    /**
     * 给Tab按钮设置图标和文字
     */
    private View getTabItemView(int index) {
        View view = layoutInflater.inflate(R.layout.tab_item_view, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.iv_tab);
        imageView.setImageResource(tabImages[index]);

        TextView textView = (TextView) view.findViewById(R.id.tv_tab);
        textView.setText(tabTexts[index]);

        return view;
    }


}
